All files / web/src/app/api/family/children/[playerId]/parents/[parentUserId] route.ts

0% Statements 0/45
0% Branches 0/1
0% Functions 0/1
0% Lines 0/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                                                                           
import { NextResponse } from 'next/server'
import { isParentOf } from '@/lib/classroom'
import { unlinkParentFromChild } from '@/lib/classroom/family-manager'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * DELETE /api/family/children/[playerId]/parents/[parentUserId]
 * Remove another parent's access to a child.
 *
 * Authorization: caller must be a parent of this child.
 * Safety: cannot remove the last parent (enforced by unlinkParentFromChild).
 */
export const DELETE = withAuth(async (_request, { userId, params }) => {
  try {
    const { playerId, parentUserId } = (await params) as {
      playerId: string
      parentUserId: string
    }

    // Caller must be a parent of this child
    const callerIsParent = await isParentOf(userId, playerId)
    if (!callerIsParent) {
      return NextResponse.json({ error: 'You must be a parent of this student' }, { status: 403 })
    }

    // Cannot remove yourself via this route (different concept)
    if (parentUserId === userId) {
      return NextResponse.json(
        { error: 'Cannot remove yourself. Use a different workflow to leave.' },
        { status: 400 }
      )
    }

    const result = await unlinkParentFromChild(parentUserId, playerId, userId)

    if (!result.success) {
      return NextResponse.json({ error: result.error }, { status: 400 })
    }

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('Failed to unlink parent from child:', error)
    return NextResponse.json({ error: 'Failed to remove parent access' }, { status: 500 })
  }
})